package com.cb.domain.model.system;


import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;

/**
 * SystemUser Base Java Bean, base class for the model, mapped directly to database table
 * 
 * Avoid changing this file if not necessary, will be overwritten. 
 *
 * TODO: add class/table comments
 */
@Entity
@Table(name="system_user")
public class SystemUser implements UserDetails {

    protected Long systemUserId;
	protected String username;
	protected String password;
	protected String email;
	protected String phone;
	protected String mobile;
	protected String fullname;
	protected Short sex;
	protected java.util.Date createTime;
	protected Short status;
	protected java.util.Set<AppRole> roles = new java.util.HashSet();

    
	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name="system_user_role",joinColumns={@JoinColumn(name="systemUserId")},inverseJoinColumns={@JoinColumn(name="roleId")})
	public java.util.Set<AppRole> getRoles() {
		return roles;
	}

	public void setRoles(java.util.Set<AppRole> roles) {
		this.roles = roles;
	}
	
	/**
	 * 用户状态status
	 * 0=禁用
	 * 
	 */
	public static Short FORBIDDEN_STATUS=0;
	/**
	 * 用户状态status
	 * 1=启用
	 */
	public static Short ACTIVATION_STATUS=1;
	/**
	 * 超管用户
	 */
	public static Short USER_TYPE_SUPER_ADMIN=1;
	/**
	 * 普通用户
	 */
	public static Short USER_TYPE_MEMBER=1;

	@Transient
	public boolean isCorrectPwd(String password) {
		if (StringUtils.isBlank(this.password))
			throw new UnsupportedOperationException("password is unloaded");
		return this.password.equals(password);
	}
	
	/**
	 * 为登录用户加上一个登录后可以访问的角色
	 */
	@Transient
	public GrantedAuthority[] getAuthorities() {
		GrantedAuthority[] granted=(GrantedAuthority[]) roles.toArray(new GrantedAuthority[roles.size()+2]);
//		if(USER_TYPE_SUPER_ADMIN.equals(userType)){//企业用户
//			granted[granted.length-2]=new GrantedAuthorityImpl(AppRole.ROLE_ADMINISTRATOR);
//		}else if(USER_TYPE_MEMBER.equals(userType)){//个人用户
//			granted[granted.length-2]=new GrantedAuthorityImpl(AppRole.ROLE_MEMBER);
//		}
		granted[granted.length-2]=new GrantedAuthorityImpl(AppRole.ROLE_PUBLIC);
		granted[granted.length-1]=new GrantedAuthorityImpl(AppRole.ROLE_ADMIN);
		return granted;
	}

	@Transient
	public synchronized boolean isCredentialsNonExpired() {
		return true;
	}

	@Transient
	public boolean isAccountNonExpired() {
		return true;
	}

	@Transient
	public boolean isAccountNonLocked() {
		return true;
	}

	@Transient
	public boolean isEnabled() {
		return ACTIVATION_STATUS.equals(status);
	}


	/**
	 * 	 * @return Long
     * @hibernate.id column="systemUserId" type="java.lang.Long" generator-class="native"
	 */
	
	@Id     
    @GeneratedValue(generator="Id_Generator")
    @GenericGenerator(name="Id_Generator", strategy="com.cb.common.util.IdGenerator")
	public Long getSystemUserId() {
		return this.systemUserId;
	}
	
	/**
	 * Set the systemUserId
	 */	
	public void setSystemUserId(Long aValue) {
		this.systemUserId = aValue;
	}	

	
	@Column(name = "userName")	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(name = "passWord")		
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * 	 * @return String
	 * @hibernate.property column="email" type="java.lang.String" length="256" not-null="false" unique="false"
	 */
	
	@Column(name = "email")	
	public String getEmail() {
		return this.email;
	}
	
	/**
	 * Set the email
	 */	
	public void setEmail(String aValue) {
		this.email = aValue;
	}	

	/**
	 * 	 * @return String
	 * @hibernate.property column="phone" type="java.lang.String" length="256" not-null="false" unique="false"
	 */
	
	@Column(name = "phone")	
	public String getPhone() {
		return this.phone;
	}
	
	/**
	 * Set the phone
	 */	
	public void setPhone(String aValue) {
		this.phone = aValue;
	}	

	/**
	 * 	 * @return String
	 * @hibernate.property column="mobile" type="java.lang.String" length="256" not-null="false" unique="false"
	 */
	
	@Column(name = "mobile")	
	public String getMobile() {
		return this.mobile;
	}
	
	/**
	 * Set the mobile
	 */	
	public void setMobile(String aValue) {
		this.mobile = aValue;
	}	

	/**
	 * 	 * @return String
	 * @hibernate.property column="fullname" type="java.lang.String" length="256" not-null="false" unique="false"
	 */
	
	@Column(name = "fullname")	
	public String getFullname() {
		return this.fullname;
	}
	
	/**
	 * Set the fullname
	 */	
	public void setFullname(String aValue) {
		this.fullname = aValue;
	}	

	/**
	 * sex=1 男
            sex=0女	 * @return Short
	 * @hibernate.property column="sex" type="java.lang.Short" length="5" not-null="false" unique="false"
	 */
	
	@Column(name = "sex")	
	public Short getSex() {
		return this.sex;
	}
	
	/**
	 * Set the sex
	 */	
	public void setSex(Short aValue) {
		this.sex = aValue;
	}	

	/**
	 * 	 * @return java.util.Date
	 * @hibernate.property column="createTime" type="java.util.Date" length="19" not-null="false" unique="false"
	 */
	
	@Column(name = "createTime")	
	public java.util.Date getCreateTime() {
		return this.createTime;
	}
	
	/**
	 * Set the createTime
	 */	
	public void setCreateTime(java.util.Date aValue) {
		this.createTime = aValue;
	}	

	/**
	 * status=1 可用
            status=0 不可用	 * @return Short
	 * @hibernate.property column="status" type="java.lang.Short" length="5" not-null="false" unique="false"
	 */
	
	@Column(name = "status")	
	public Short getStatus() {
		return this.status;
	}
	
	/**
	 * Set the status
	 */	
	public void setStatus(Short aValue) {
		this.status = aValue;
	}	

}